package com.lihan.GetSeatInLib.Service.impl;

import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.lihan.GetSeatInLib.Domain.LibTable;
import com.lihan.GetSeatInLib.Domain.SeatModel;
import com.lihan.GetSeatInLib.Domain.Enums.SeatStaus;
import com.lihan.GetSeatInLib.Repository.SeatRepository;
import com.lihan.GetSeatInLib.Service.LibTableService;
import com.lihan.GetSeatInLib.Service.SeatService;


/**
 * 
 * @ClassName: SeatSeriveImpl
 * @Description: 座位接口的实现类
 * @author ASUS
 * @date 2022年2月28日
 *  tips: alt + shift + z 快速try-catch
 *  	  ctrl + 1 +enter 快速抛异常
 */
@Service
public class SeatSeriveImpl implements SeatService {
	
	//注入持久层
	@Autowired
	private SeatRepository _seatdao;
	
	@Autowired
	private LibTableService _tabledao;
	
	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, List<SeatModel>> redis;
	
	/**
	 * 根据所属的桌子查找座位
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SeatModel> SearchByLibTabel(LibTable table) {
		ValueOperations<String, List<SeatModel>> ops = redis.opsForValue();
		if(redis.hasKey("桌子"+table.getID()+"的座位缓存"))
			return (List<SeatModel>) ops.get("桌子"+table.getID()+"的座位缓存");
		//先查找Redis缓存
		List<SeatModel> list = _seatdao.SearchSeatByTable(table);
		ops.set("桌子"+table.getID()+"的座位缓存", list); //没有就放到缓存
		return list;
	}
	
	@Override
	public boolean CreatNewSeat(SeatModel seat) {
		if(_seatdao.save(seat) != null)
			return true;
		else
			return false;
	}
	
	@Override
	public boolean DeleteById(Long id) {
		try {
			_seatdao.deleteById(id);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
	
	//查
	@Override
	public SeatModel getSeatById(Long id) {
		return _seatdao.getById(id);
	}
	
	//改
	@Override
	public boolean UpdateSeat(SeatModel seat) {
		try {
			_seatdao.save(seat);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	//锁定单个座位(本类维护关系)
	@Override
	public boolean LockSeat(Long ID) {
		try {
			SeatModel seat = _seatdao.getById(ID);
			if(seat.getStatus()==SeatStaus.FREE) {
				seat.setStatus(SeatStaus.LOCKED);
				return true;
			}
			
		}catch (Exception e) {
		}
		return false;
	}
	
	//锁定单个座位(关联类维护关系)
	@Override
	public boolean LockSeat(Long BelongTableID, Long ID) {
		if(BelongTableID.equals("")) //如果没有传所属的桌子ID，就和上个方法一样
			LockSeat(ID);
		LibTable table = _tabledao.getTibTableById(BelongTableID);
		
		Set<SeatModel> seats = table.getSeatlist();
		
		for(SeatModel item:table.getSeatlist()) {
			if(item.getID()==ID&&item.getStatus()==SeatStaus.FREE) {
				item.setStatus(SeatStaus.LOCKED);
				return true;
			}
		}
		return false;
	}
	
	//解锁一个座位(本类维护关系)
	@Override
	public boolean UnLockSeat(Long ID) {
		try {
			SeatModel seat = _seatdao.getById(ID);
			if(seat.getStatus()==SeatStaus.LOCKED)
				seat.setStatus(SeatStaus.FREE);
			return true;
		}catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	//解锁一个座位(关联类维护关系)
	@Override
	public boolean UnLockSeat(Long BelongTableID, Long ID) {
		if(BelongTableID.equals("")) //如果没有传所属的桌子ID，就和上个方法一样
			UnLockSeat(ID);
		LibTable table = _tabledao.getTibTableById(BelongTableID);
		
		Set<SeatModel> seats = table.getSeatlist();
		
		for(SeatModel item:table.getSeatlist()) {
			if(item.getID()==ID&&item.getStatus()==SeatStaus.LOCKED) {
				item.setStatus(SeatStaus.FREE);
				return true;
			}
		}
		return false;
	}

	//让一个座位改为使用中的状态
	@Override
	public boolean SeatInUsed(Long ID) {
		
		SeatModel seat = _seatdao.getById(ID);
		if(seat.getStatus()==SeatStaus.FREE) {
			seat.setStatus(SeatStaus.USED);
			_seatdao.save(seat);
			return true;
		}
		return false;
	}
	
	@Override
	public boolean UnSeatInUsed(Long ID) {
		SeatModel seat = _seatdao.getById(ID);
		if(seat.getStatus()==SeatStaus.USED) {
			seat.setStatus(SeatStaus.FREE);
			_seatdao.save(seat);
			return true;
		}
		return false;
	}
}
